package com.laowang.project.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.laowang.common.utils.SecurityUtils;
import com.laowang.project.system.domain.MyCyhPartnerVipNum;
import com.laowang.project.system.domain.MyCyhProVipNum;
import com.laowang.project.system.domain.WxUser;
import com.laowang.project.system.service.WxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.MyCyhGladTiding;
import com.laowang.project.system.service.MyCyhGladTidingService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

/**
 * 喜报Controller
 * 
 * @author laowang
 * @date 2024-03-28
 */
@Api(tags = "喜报接口")
@RestController
@RequestMapping("/gladTiding")
public class MyCyhGladTidingController extends BaseController {
    @Autowired
    private MyCyhGladTidingService myCyhGladTidingService;

    @Autowired
    private WxUserService wxUserService;

    @ApiOperation(value = "小程序 查询自己的喜报 ",notes = "")
    @GetMapping("/web/me")
    public AjaxResult listme( Integer pageNum,Integer pageSize) {

        if (pageNum == null && pageSize == null){
            pageNum =1;
            pageSize = 10;
        }
        PageHelper.startPage(pageNum,pageSize);
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        WxUser wxUser = wxUserService.selectWxUserById(mid);
        Boolean flag = false;
        if (wxUser.getIsPartner() !=null && wxUser.getIsPartner().equals(1) && wxUser.getPartnerVip() !=null && wxUser.getExpireTime2().after(new Date())){
            flag = true;
        }else if (wxUser.getPromoDor() !=null && wxUser.getPromoDor().equals(1) && wxUser.getPromoLevel()!=null && wxUser.getExpireTime1() !=null && wxUser.getExpireTime1().after(new Date())) {
            flag = true;
        }
        List<MyCyhGladTiding> myCyhGladTidings = new ArrayList<>();
        if (flag){
            MyCyhGladTiding myCyhGladTiding = new MyCyhGladTiding();
            myCyhGladTiding.setUserId(mid);
            myCyhGladTidings = myCyhGladTidingService.selectMyCyhGladTidingList(myCyhGladTiding);
        }

        return AjaxResult.success(myCyhGladTidings);
    }

    /**
     * 查询喜报列表
     */
    @ApiOperation(value = "查询喜报列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:gladTiding:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyCyhGladTiding myCyhGladTiding) {
        startPage();
        List<MyCyhGladTiding> list = myCyhGladTidingService.selectMyCyhGladTidingList(myCyhGladTiding);
        return getDataTable(list);
    }
    
    /**
     * 导出喜报列表
     */
    @ApiOperation(value = "导出喜报列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:gladTiding:export')")
    @Log(title = "喜报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyCyhGladTiding myCyhGladTiding) {
        List<MyCyhGladTiding> list = myCyhGladTidingService.selectMyCyhGladTidingList(myCyhGladTiding);
        ExcelUtil<MyCyhGladTiding> util = new ExcelUtil<MyCyhGladTiding>(MyCyhGladTiding.class);
        return util.exportExcel(list, "gladTiding");
    }

    /**
     * 获取喜报详细信息
     */
    @ApiOperation(value = "获取喜报详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('my:gladTiding:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(myCyhGladTidingService.selectMyCyhGladTidingById(id));
    }

    /**
     * 新增喜报
     */
    @ApiOperation(value = "新增喜报",notes = "")
    @PreAuthorize("@ss.hasPermi('my:gladTiding:add')")
    @Log(title = "喜报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCyhGladTiding myCyhGladTiding) {
        return toAjax(myCyhGladTidingService.insertMyCyhGladTiding(myCyhGladTiding));
    }

    /**
     * 修改喜报
     */
    @ApiOperation(value = "修改喜报",notes = "")
    @PreAuthorize("@ss.hasPermi('my:gladTiding:edit')")
    @Log(title = "喜报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCyhGladTiding myCyhGladTiding) {
        return toAjax(myCyhGladTidingService.updateMyCyhGladTiding(myCyhGladTiding));
    }

    /**
     * 删除喜报
     */
    @ApiOperation(value = "删除喜报",notes = "")
    @PreAuthorize("@ss.hasPermi('my:gladTiding:remove')")
    @Log(title = "喜报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(myCyhGladTidingService.deleteMyCyhGladTidingByIds(ids));
    }
}
